package com.it;

import com.it.bean.R;
import com.it.bean.RestStatusEnum;
import com.it.pojo.Comment;
import com.it.pojo.params.comment.CommentQuery;
import com.it.pojo.vo.comment.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("comment")
@Api(value = "v1", tags = "评论相关接口")
public class CommentController {

    @Resource
    private CommentService commentService;


    @GetMapping("{articleid}/{pagenum}/{limit}")
    @ApiOperation(value = "根据文章id查询评论信息", notes = "根据文章id查询评论信息")

    public R<?> list(@PathVariable("articleid") Integer articleId, @PathVariable("pagenum") Integer pageNum,
                     @PathVariable("limit") Integer limit
    ) {
        if (articleId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        if (pageNum < 1 || limit < 1) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        Integer start = (pageNum - 1) * limit;
        Map<String, Object> map = commentService.listComments(articleId, start, limit);
        return R.success("成功", map);
    }

    @PostMapping("")
    public R<?> save(@RequestBody CommentQuery commentQuery) {
        System.out.println("commentQuery = " + commentQuery);
        if (commentQuery == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentQuery, comment);
        return commentService.insertComment(comment) > 0 ? R.success() : R.error();
    }

    @DeleteMapping("{commentid}")
    public R<?> delete(@PathVariable("commentid") Integer commentId) {
        if (commentId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        return commentService.deleteCommentById(commentId) > 0 ? R.success() : R.error();
    }


}
